package com.hr.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.mes.dto.Page;
import com.hr.mes.entity.Permission;
import com.hr.mes.mapper.PermissionMapper;
import com.hr.mes.mapper.RolePermissionMapper;
import com.hr.mes.service.PermissionService;
import com.hr.mes.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void addPermission(Permission permission) {
        permissionMapper.addPermission(permission);
    }

    @Override
    public void deletePermission(Integer permissionId) {
        permissionMapper.deletePermission(permissionId);
        rolePermissionMapper.deleteRolePermissionByPermissionId(permissionId);
    }

    @Override
    public void updatePermission(Permission permission) {
        permissionMapper.updatePermission(permission);
    }

    @Override
    public Page<Permission> getPermissionPage(Integer pageNo, Integer pageSize) {
        List<Permission> permissionList = permissionMapper.getPermissionList();
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Permission> pageInfo = new PageInfo<>(permissionList);
        Page<Permission> permissionPage = PageUtil.ConvertPageInfoToPage(pageInfo);
        PageHelper.clearPage();
        return permissionPage;
    }

    @Override
    public Permission getPermission(Integer permissionId) {
        Permission permission = permissionMapper.getPermission(permissionId);
        return permission;
    }
}
